﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using System.Linq;
namespace WPFExcelReport
{
    public partial class TWS04
    {

        public TWS04()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VSTUELI();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (EXAUF.Length == 0)
            {
                return;
            }
            INFO = "请定义成份";
            DESS = "";
            COLORIT = "";
             ART = EXAUF.Substring(1, 20);
            VAR = EXAUF.Substring(21, 2);
            int idx = ART.IndexOf("/");
            if(idx<0)
            {
                idx = ART.IndexOf("_");
            }
            if (idx > 0)
                DESS = ART.Substring(0, idx);
            dwData = TBMain.NewRow();
            dwData["STUELIMANDANT"] = app.Mandant;
            dwData["STUELIPKEN"] = "3";
            dwData["STUELISAPD"] = "00000000      " + (ART + VAR).PadRight(24);
            SResult = TMV.QSTU01(dwData);
            MergeData(SResult, true);
            TBMain.AcceptChanges();
            GetInfo();
            base.OnEXAUFChg(vlaue);
        }
        public double GetInfo()
        {
            double RATIO = 0;
            if (TBMain.Rows.Count == 0)
                return 0;
            foreach (DataRow row in TBMain.Rows)
            {
                string ratio = row["STUELIDATEN"].ToString().Substring(0, 4);
                row["RATIO"] = ratio;
                RATIO += double.Parse(ratio) * 0.1;
                INFO += row["STUELISASB"].ToString().Trim() + ":" + (double.Parse(ratio) * 0.1).ToString() + "%";
            }
            return RATIO;
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            int pos = 0;
            if (TBMain.Rows.Count > 0)
            {
                var gapos = (from DataRow row in TBMain.AsEnumerable()
                             select row["STUELIPPOS"].ToString())
                             .Max();
                pos = Convert.ToInt16((string)gapos);
            }
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["STUELIMANDANT"] = app.Mandant;
            dwData["STUELIPKEN"] = "3";
            dwData["STUELISAPD"] = "00000000      " + (ART + VAR).PadRight(24);
            dwData["STUELIPPOS"] = Helper.Right("00" + (pos + 1).ToString(), 2);
            TMV.InsertSTUELI(dwData);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
        public override void OnLFDChg()
        {
            if (dwMaster == null)
                return;
            dwMaster["RATIO"] = LFD;
            TMV.STUELIDATEN = Helper.Right("0000" + LFD, 4) + TMV.STUELIDATEN.Substring(4);
            base.OnLFDChg();
        }
        public override void OnKRS()
        {
            if (KRS.Length == 0)
                return;
            if (dwMaster == null)
                return;
            TMV.STUELISASB = KRS.PadRight(38);
            base.OnKRS();
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            double RATIO = GetInfo();
            if (RATIO != 100)
            {
                MessageBox.Show("100%?");
                return;
            }

            int i = 1;
            PM["ART"] = ART;
            PM["VAR"] = VAR;
            foreach (DataRow row in TBMain.Rows)
            {
                PM["COMS"] = i;
                PM["VAAGESANT" + i] = Helper.Right("000" + System.Convert.ToInt16((double.Parse(row["RATIO"].ToString()) * 0.1)).ToString(), 3);
                PM["VAAGESBEZ" + i] = row["STUELISASB"].ToString();
                i++;
                TMV.UpdateSTUELI(row, 0);
            }
            TMV.VAT.CHGCOM(PM);

        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (TBMain.Rows.Count < 2)
            {
                MessageBox.Show("不可以");
                return;
            }
            filter = "STUELIPPOS='" + dwMaster["STUELIPPOS"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                TMV.DSTU02(row);
                row.Delete();

            }
           TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
         
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (COLORIT.Trim().Length == 0)
                return;
            dwData = TBMain.NewRow();
            dwData["STUELIMANDANT"] = app.Mandant;
            dwData["STUELIPKEN"] = "3";
            dwData["STUELISAPD"] = "00000000      " + COLORIT.Substring(1,22);
            SResult = TMV.QSTU01(dwData);
            foreach(DataRow row in SResult.Tables[0].Copy().Rows)
            {
                row["STUELISAPD"] = "00000000      " + (ART + VAR).PadRight(24);
                TMV.InsertSTUELI(row);
            }
            string key = EXAUF;
            EXAUF = "";
            EXAUF = key;
            base.CopyCommand(sender, e);
        }
    }
}
